package com.changzhi.util;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 国际短信
 * @author leeqico
 */
public class SmsGlobeUtil {

    private static final Logger logger = LoggerFactory.getLogger(SmsGlobeUtil.class);

    public static boolean sendMsg(String mobile, String message) {
        //初始化acsClient，<accessKeyId>和<accessSecret>"在短信控制台查询即可
        DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", "LTAI5tKyoudzqj64FNceh4BK", "YyKJzpxdTqgEwaOa8UDmyeOzNrEBSH");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        //域名，请勿修改
        request.setSysDomain("dysmsapi.ap-southeast-1.aliyuncs.com");
        //API版本号，请勿修改
        request.setSysVersion("2018-05-01");
        //API名称
        request.setSysAction("SendMessageToGlobe");
        //接收号码，格式为：国际码+号码，必填85263760175
        request.putQueryParameter("To", mobile);
        //发送方senderId，选填
        //request.putQueryParameter("From", "1234567890");
        //短信内容，必填
        String content = "【Boba助手】验证码："+message+"（10分钟内有效），请勿泄露!";
        request.putQueryParameter("Message", message);
        try {
            logger.info("短信发送手机：{}，内容：{}", mobile, message);
            CommonResponse response = client.getCommonResponse(request);
            String s = JSON.toJSONString(response);
            logger.info("短信发送结果：{}，手机：{}", s, mobile);
            return response.getHttpStatus() == 200;
        } catch (Exception e) {
            logger.error("短信发送失败，手机：{}，内容：{},异常：{}", mobile, message, e.getMessage());
            e.printStackTrace();
        }
        return false;
    }


}
